package com.base.hong.widget

import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import com.base.hong.R


/**
 * @ClassName CustomView
 * @Description TODO
 * @Author Hong
 * @Date 2022/10/11 14:41
 * @Version 1.0
 *
 */
class CustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    var typedArray = context.obtainStyledAttributes(attrs, R.styleable.custom)
    var mOuterColor = Color.GREEN
    var mInnerColor = Color.BLUE
    var mBorderWidth = dp2px(20f)
    var mCurrentStep = 500
    var mStepTextColor = Color.RED
    var mStepTextSize = 100f

    val mOuterPaint by lazy {
        Paint().also {
            it.isAntiAlias = true
            it.color = mOuterColor
            it.strokeWidth = mBorderWidth
            it.style = Paint.Style.STROKE
            it.strokeCap = Paint.Cap.ROUND
        }
    }
    val mInnerPaint by lazy {
        Paint().also {
            it.isAntiAlias = true
            it.color = mInnerColor
            it.strokeWidth = mBorderWidth
            it.style = Paint.Style.STROKE
            it.strokeCap = Paint.Cap.ROUND
        }
    }
    val mTextPaint by lazy {
        Paint().also {
            it.isAntiAlias = true
            it.color = mStepTextColor
            it.textSize = mStepTextSize
            it.textAlign = Paint.Align.CENTER
        }
    }

    init {
        //1 分析需求
        //2 自定义属性
        //3 onMeasure
        //4 获得自定义属性
        //5 绘制
        mOuterColor = typedArray.getColor(R.styleable.custom_outer_color, mOuterColor)
        mInnerColor = typedArray.getColor(R.styleable.custom_innner_color, mInnerColor)
        mBorderWidth = typedArray.getDimension(R.styleable.custom_border_width, mBorderWidth)
        mStepTextColor = typedArray.getColor(R.styleable.custom_step_text_color, mStepTextColor)
        mStepTextSize =
            typedArray.getDimensionPixelSize(
                R.styleable.custom_step_text_size,
                mStepTextSize.toInt()
            ).toFloat()
        typedArray.recycle()

    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        setMeasuredDimension(
            if (width > height) height else width,
            if (width > height) height else width
        )

    }

    override fun onDraw(canvas: Canvas?) {
//        canvas?.drawLine(0f, width / 2f, width.toFloat(), width / 2f, mOuterPaint)
//        canvas?.drawLine(width / 2f, 0f, width / 2f, width.toFloat(), mOuterPaint)
        //绘制内圆弧
        val rectF = RectF(
            mBorderWidth / 2,
            mBorderWidth / 2,
            width.toFloat() - mBorderWidth / 2,
            width.toFloat() - mBorderWidth / 2
        )
//        canvas?.drawRect(rectF,mOuterPaint)
        canvas?.drawArc(rectF, 135f, 270f, false, mOuterPaint)
        canvas?.drawArc(rectF, 135f, (mCurrentRadio / mMaxRadio) * 270f, false, mInnerPaint)
        //画文字
        var fontMetrics = Paint.FontMetrics()
        mTextPaint.getFontMetrics(fontMetrics)
        canvas?.drawText(
            mCurrentRadio.toInt().toString(),
            width / 2f,
            width / 2f - (fontMetrics.ascent + fontMetrics.descent) / 2,
            mTextPaint
        )

    }

    var mMaxRadio = 1000f
    var mCurrentRadio = 1f

    fun setMaxRadio(float: Float) {
        this.mMaxRadio = float
    }

    fun setCurrentRadio(float: Float) {
        this.mCurrentRadio = float
        invalidate()
    }


    private fun dp2px(dp: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dp,
            Resources.getSystem().displayMetrics
        )
    }
}